<?php
/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 2018/4/5
 * Time: 15:50
 */

namespace App\Services;


use App\Models\Cards;
use App\Models\CardSeries;
use App\Models\Member;
use App\Models\UserDrawLotsLog;
use App\Repos\MemberRepo;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
use Carbon\Carbon;

class DrawLotsService
{
    protected $memberRepo;
    
    public function __construct(MemberRepo $memberRepo)
    {
        $this->memberRepo=$memberRepo;
    }

    /**
     * �û���ǩ
     * @param $user_id
     * @return array
     */
    public function drawLots($user_id){
        $count = UserDrawLotsLog::where(['user_id'=>$user_id])->whereDate('created_at', date('Y-m-d',time()))->count();
        if($count > 0){
            $data['day_checked'] = true;
            return $data;
        }
        $data['day_checked'] = false;
        $luckyScore = mt_rand(5,10);
        //Ϊmember���һ��ǩ���ֶ�   checkInNum = 0
        Member::where('uid',$user_id)->increment('checkInNum');
        $userDrawLotsLog = new UserDrawLotsLog();
        $userDrawLotsLog->setAttribute('user_id',$user_id);
        $userDrawLotsLog->setAttribute('lucky_type',0);
        $userDrawLotsLog->save();
        $user = Member::where('uid',$user_id)->first();
        $data['checkInScore'] = $luckyScore;
        $data['luckyType'] = 0;
        $data['continueCheckInDays'] = $user['checkInNum']?:0;
        $data['continueCheckInScore'] = 0;
        //���ǩ������
        $this->memberRepo->incrementUserRemoteScoreForSign($user,$data['checkInScore']+$data['continueCheckInScore']);
        return $data;
    }

    public static function get_card_series()
    {
        $cart_series=CardSeries::get()->where('is_open',1);
        return $cart_series;
    }

    public static function getCard($user_id,$card_series_id)
    {
        $card_model = Cards::valid();
        $all_rand = $card_model->where('endTime','>',Carbon::now()->toDateTimeString())->sum('probability');
        $lottery_rand = mt_rand(1, $all_rand);
        $hit_lottery = null;

        $cards = $card_model->where("card_series_id",$card_series_id)->inRandomOrder()->get();
         $level_left = 0;
        foreach ($cards as $card) {
            $level_right = $card['probability'] + $level_left;
            if ($lottery_rand > $level_left && $lottery_rand <= $level_right) {
                $hit_lottery = $card;
                break;
            }
            $level_left = $level_right;
        }
        return $hit_lottery;
    }
    
    public static function getRandomCard($user_id)
    {
        $card_model = Cards::valid();
        //不获取已经获取过的
        //$card_ids = UserDrawLotsLog::where('user_id',$user_id)->whereNotNull('card_id')->pluck('card_id');
        //if(!$card_ids->isEmpty()){
        //    $card_model = $card_model->whereNotIn('id',$card_ids);
        //}
        //按库存设置中奖率
        $card_series = (int)CardSeries::count();
        $all_stock = 100 * $card_series;
    
        $lottery_rand = mt_rand(1,$all_stock);
        $hit_lottery=null;
        $cards = $card_model->inRandomOrder()->get();
        $level_left = 0;
        foreach ($cards as $card) {
            $level_right = $card['probability'] + $level_left;
    
            //dd(compact('level_left','level_right','lottery_rand'));
            
            if($lottery_rand > $level_left && $lottery_rand <= $level_right){
                $hit_lottery = $card;
                break;
            }
            $level_left = $level_right;
        }
        return $hit_lottery;
        //return Cards::inRandomOrder()->first();
    }
    
    public static function checkSignStatus($user_id)
    {
        return UserDrawLotsLog::where(['user_id'=>$user_id])
            ->whereDate('created_at', date('Y-m-d'))->exists();
    }
    
    public static function checkSignSendedStatus($user_id)
    {
        return UserDrawLotsLog::where(['user_id'=>$user_id])
            ->whereNotNull('card_id')
            ->whereDate('created_at', date('Y-m-d'))->exists();
    }
}